/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample.opengl;

import pl.droidsonroids.gif.gifsample.LogUtil;

import ohos.agp.components.Image;
import ohos.agp.graphics.SurfaceOps;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * SurfaceCallback
 */
public class SurfaceCallback implements SurfaceOps.Callback {
    private static final String TAG = SurfaceCallback.class.getName();
    private final GifTexImage2dProgram gifTexImage2dProgram;
    private final EglConnection eglConnection = new EglConnection();
    private ScheduledThreadPoolExecutor mExecutor = new ScheduledThreadPoolExecutor(1);
    private Draw2DTask draw2dTask = new Draw2DTask();
    private EventHandler handler = new Draw2dHandler();

    /**
     * Constructor
     *
     * @param gifTexImage2dProgram gifTexImage2dProgram
     * */
    public SurfaceCallback(GifTexImage2dProgram gifTexImage2dProgram) {
        this.gifTexImage2dProgram = gifTexImage2dProgram;
    }

    @Override
    public void surfaceCreated(SurfaceOps surfaceOps) {
        eglConnection.destroy();
        try {
            eglConnection.init(surfaceOps.getSurface());
        } catch (Throwable throwable) {
            LogUtil.error(TAG, "Exception");
        }
        gifTexImage2dProgram.initialize();
    }

    @Override
    public void surfaceChanged(SurfaceOps surfaceOps, int i1, int i2, int i3) {
        gifTexImage2dProgram.setDimensions(i2, i3, Image.ScaleMode.STRETCH);
        int count = 0;
        while (count < 10) {
            gifTexImage2dProgram.draw();
            eglConnection.draw();
            try {
                Thread.sleep(240);
            } catch (InterruptedException e) {
                LogUtil.debug(TAG, "InterruptedException");
            }
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceOps surfaceOps) {
        gifTexImage2dProgram.destroy();
        eglConnection.destroy();
    }

    class Draw2DTask implements Runnable {
        @Override
        public void run() {
            gifTexImage2dProgram.draw();
            eglConnection.draw();
            draw2dTask = new Draw2DTask();
            mExecutor.schedule(draw2dTask, 420, TimeUnit.MILLISECONDS);
        }
    }

    class Draw2dHandler extends EventHandler {
        Draw2dHandler() {
            super(EventRunner.getMainEventRunner());
        }

        @Override
        protected void processEvent(InnerEvent event) {
            gifTexImage2dProgram.draw();
            eglConnection.draw();
            if (hasInnerEvent(1)) {
                removeEvent(1);
            }
            this.sendEvent(1, 240);
        }
    }
}
